package servlet;

import bean.Result;
import biz.EmployeeBiz;
import com.google.gson.Gson;
import dao.EmployeeDao;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Map;

@WebServlet(name = "QueryEmployeeServlet", value = "/QueryEmployeeServlet.s")
public class QueryEmployeeServlet extends HttpServlet {
    private EmployeeBiz biz = new EmployeeBiz();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Result result;

        try {
            String position = request.getParameter("position");
            String department = request.getParameter("department");
            String currentPage = request.getParameter("currentPage");
            String pageSize = request.getParameter("pageSize");

            int page = Integer.valueOf(currentPage);
            int size = Integer.valueOf(pageSize);
            System.out.println(page);

            Map<String, Object> res = biz.query(position, department, size * (page - 1), size);

            result = new Result(1, "查询成功!", res);
        } catch (Exception e) {
            e.printStackTrace();
            result = new Result(0, "查询失败!", null);
        }
        System.out.println(result.toString());
        String json = new Gson().toJson(result);
        response.getWriter().append(json);


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
